package cn.javabb.project.controller;

import cn.javabb.common.model.domain.AjaxResult;
import cn.javabb.framework.web.controller.BaseController;
import cn.javabb.framework.web.page.TableDataInfo;
import cn.javabb.project.entity.SysTemplate;
import cn.javabb.project.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2021/05/04 20:47
 */
@Api(tags = "模板管理")
@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController {
    @Autowired
    private TemplateService templateService;

    @ApiOperation("分页查询模版")
    @GetMapping("/")
    public TableDataInfo list(SysTemplate dsc) {
        startPage();
        List<SysTemplate> list = templateService.list(dsc);
        return getDataTable(list);
    }
    @ApiOperation("根据模版组分页查询模版")
    @GetMapping("/group/{id}")
    public TableDataInfo listByGroupId(@PathVariable("id") Long id) {
        startPage();
        List<SysTemplate> list = templateService.listByGroupId(id);
        return getDataTable(list);
    }
    @ApiOperation("根据ID查询模版")
    @GetMapping("/{id}")
    public AjaxResult getOne(@PathVariable Long id) {
        return AjaxResult.success(templateService.getById(id));
    }
    @ApiOperation("新增模版")
    @PostMapping("/")
    public AjaxResult save(@RequestBody SysTemplate dsc) {
        dsc.setStatus("0");
        dsc.setCreateTime(new Date());
        dsc.setUpdateTime(new Date());
        if (templateService.save(dsc)) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.error("保存失败");
    }
    @ApiOperation("更新模版")
    @PutMapping("/")
    public AjaxResult update(@RequestBody SysTemplate dsc) {
        dsc.setUpdateTime(new Date());
        if (templateService.updateById(dsc)) {
            return AjaxResult.success("更新成功");
        }
        return AjaxResult.error("更新失败");
    }
    @ApiOperation("删除模版")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (templateService.removeById(id)) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }


}
